﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HaumDisplay
{
    class Tools
    {
        /// <summary>
        /// 获取两点之间的角度
        /// </summary>
        /// <param name="center"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static double GetRotate(PointF center, PointF p)
        {
            // 计算向量的 x 和 y 分量
            float dx = p.X - center.X;
            float dy = p.Y - center.Y;
            // 使用 Math.Atan2 函数计算角度（弧度）
            double angleRad = Math.Atan2(dy, dx);
            // 将弧度转换为角度
            float angleDeg = (float)(angleRad * (180 / Math.PI));
            return angleDeg;
        }

        /// <summary>
        /// 求某一点以中心点进行旋转后的坐标
        /// </summary>
        /// <param name="point"></param>
        /// <param name="center"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static PointF RotatePoint(PointF point, PointF center, double angle)
        {
            // 将角度转换为弧度
            double radians = angle * (float)(Math.PI / 180);
            float cosAngle = (float)Math.Cos(radians);
            float sinAngle = (float)Math.Sin(radians);

            // 计算平移到原点后的坐标
            float translatedX = point.X - center.X;
            float translatedY = point.Y - center.Y;

            // 进行旋转操作
            float rotatedX = translatedX * cosAngle - translatedY * sinAngle;
            float rotatedY = translatedX * sinAngle + translatedY * cosAngle;

            // 平移回原来的位置
            float newX = center.X + rotatedX;
            float newY = center.Y + rotatedY;

            return new PointF(newX, newY);
        }

        /// <summary>
        /// 查询集合中某一点相差与某一个点是否5像素以内，如果是，返回它的下标
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ps"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool QueryPath(PointF p, List<PointF> ps, out int index)
        {
            float dx = 0;
            float dy = 0;
            for (int i = 0; i < ps.Count - 1; i++)
            {
                dx = p.X - ps[i].X;
                dy = p.Y - ps[i].Y;
                if (Math.Abs(dx) < 5 && Math.Abs(dy) < 5)
                {
                    index = i;
                    return true;
                }
            }
            index = -1;
            return false;
        }

        /// <summary>
        /// 判断鼠标位置是否在图像宽高范围内
        /// </summary>
        /// <param name="point"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static bool IsMouseInImage(PointF point, Size size)
        {
            if (point.X >= 0 && point.X < size.Width && point.Y >= 0 && point.Y < size.Height)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Point ScreenTransImagePoint(Point screenPoint, float scale, PointF offset)
        {
            // 计算鼠标在原始图片上的相对位置
            float relativeX = (screenPoint.X - offset.X) / scale;
            float relativeY = (screenPoint.Y - offset.Y) / scale;
            return new Point((int)relativeX, (int)relativeY);
        }

        /// <summary>
        /// 将控件坐标点转为图像坐标点
        /// </summary>
        /// <param name="screenPoint">一个控件坐标点</param>
        /// <returns>图像坐标</returns>
        public static PointF ScreenTransImagePointF(Point screenPoint, float scale, PointF offset)
        {
            // 计算鼠标在原始图片上的相对位置
            float relativeX = (screenPoint.X - offset.X) / scale;
            float relativeY = (screenPoint.Y - offset.Y) / scale;
            return new PointF(relativeX, relativeY);
        }

        public static Dictionary<int, Bitmap> CreateThumbnails(Bitmap bmp, float ratio)
        {
            Dictionary<int, Bitmap> thums = new Dictionary<int, Bitmap>();
            float sca = 1;
            bool srcW = false;
            bool srcH = false;
            for (int i = 0; i < 3; i++)
            {
                int width = (int)(bmp.Size.Width * ratio * sca);
                int height = (int)(bmp.Size.Height * ratio * sca);
                if (width < 100 || srcW || width > bmp.Size.Width)
                {
                    srcW = true;
                    width = bmp.Width;
                }
                if (height < 100 || srcH || height > bmp.Size.Height)
                {
                    srcH = true;
                    height = bmp.Height;
                }
                Bitmap thumbnail = new Bitmap(bmp, width, height);
                thums.Add(i, thumbnail);
                sca += 2f;
            }
            return thums;

        }

        /// <summary>
        /// 检索图像编码器与解码器
        /// </summary>
        /// <param name="mimeType">"image/jpeg"</param>
        /// <returns>ImageCodecInfo / None</returns>
        public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            for (int i = 0; i < codecs.Length; i++)
            {
                if (codecs[i].MimeType == mimeType)
                {
                    return codecs[i];
                }
            }
            return null;
        }

        /// <summary>
        /// 根据文件扩展名获取图片格式
        /// </summary>
        public static ImageFormat GetImageFormat(string filePath)
        {
            string ext = System.IO.Path.GetExtension(filePath).ToLower();
            switch (ext)
            {
                case ".bmp":
                    return ImageFormat.Bmp;
                case ".jpg":
                case ".jpeg":
                    return ImageFormat.Jpeg;
                case ".png":
                    return ImageFormat.Png;
                case ".gif":
                    return ImageFormat.Gif;
                default:
                    return ImageFormat.Bmp; // 默认保存为BMP
            }
        }

        /// <summary>
        /// 将图像坐标点转为控件坐标点
        /// </summary>
        /// <param name="imagePoint">一个图像坐标点</param>
        /// <returns>控件坐标</returns>
        public static PointF ImageTransScreenPointF(PointF imagePoint, float scale, PointF offset)
        {
            float relativeX = imagePoint.X * scale + offset.X;
            float relativeY = imagePoint.Y * scale + offset.Y;
            return new PointF(relativeX, relativeY);
        }

        /// <summary>
        /// 将图像坐标点集转为控件坐标点集
        /// </summary>
        /// <param name="imagePoint">一个图像坐标点</param>
        /// <returns>控件坐标</returns>
        public static List<PointF> ImageTransScreenPointF(List<PointF> imagePoints, float scale, PointF offset)
        {
            List<PointF> ps = new List<PointF>();
            for (int i = 0; i < imagePoints.Count; i++)
            {
                PointF p = ImageTransScreenPointF(imagePoints[i], scale, offset);
                ps.Add(p);
            }
            return ps;
        }

        /// <summary>
        /// 将图像坐标点集转为控件坐标点集
        /// </summary>
        /// <param name="imagePoint">一个图像坐标点</param>
        /// <returns>控件坐标</returns>
        public static List<PointF> ImageTransScreenPointF(PointF[] imagePoints, float scale, PointF offset)
        {
            List<PointF> ps = new List<PointF>();

            for (int i = 0; i < imagePoints.Length; i++)
            {
                PointF p = ImageTransScreenPointF(imagePoints[i], scale, offset);
                ps.Add(p);
            }
            return ps;
        }

    }
}
